hast-util-select
hast utility with equivalents for querySelector
, querySelectorAll
,
and matches
.
Contents
What is this?
This package lets you find nodes in a tree, similar to how querySelector
,
querySelectorAll
, and matches
work with the DOM.
One notable difference between DOM and hast is that DOM nodes have references
to their parents, meaning that document.body.matches(':last-child')
can
be evaluated to check whether the body is the last child of its parent.
This information is not stored in hast, so selectors like that don’t work.
When should I use this?
This is a small utility that is quite useful, but is rather slow if you use it a
lot.
For each call, it has to walk the entire tree.
In some cases, walking the tree once with unist-util-visit
is smarter, such as when you want to change certain nodes.
On the other hand, this is quite powerful and fast enough for many other cases.
This utility is similar to unist-util-select
, which can
find and match any unist node.
Install
This package is ESM only.
In Node.js (version 14.14+ and 16.0+), install with npm:
npm install hast-util-select
In Deno with esm.sh
:
import {matches, select, selectAll} from "https://esm.sh/hast-util-select@5"
In browsers with esm.sh
:
<script type="module">
import {matches, select, selectAll} from "https://esm.sh/hast-util-select@5?bundle"
</script>
Use
import {h} from 'hastscript'
import {matches, select, selectAll} from 'hast-util-select'
const tree = h('section', [
h('p', 'Alpha'),
h('p', 'Bravo'),
h('h1', 'Charlie'),
h('p', 'Delta'),
h('p', 'Echo'),
h('p', 'Foxtrot'),
h('p', 'Golf')
])
matches('section', tree)
console.log(select('h1 ~ :nth-child(even)', tree))
console.log(selectAll('h1 ~ :nth-child(even)', tree))
API
This package exports the identifiers matches
, select
,
and selectAll
.
There is no default export.
matches(selector, node[, space])
Check that the given node
matches selector
.
This only checks the element itself, not the surrounding tree.
Thus, nesting in selectors is not supported (p b
, p > b
), neither are
selectors like :first-child
, etc.
This only checks that the given element matches the selector.
Parameters
selector
(string
)
— CSS selector, such as (h1
, a, b
)node
(Node
, optional)
— node that might match selector
, should be an elementspace
(Space
, default: 'html'
)
— name of namespace
Returns
Whether node
matches selector
(boolean
).
Example
import {h} from 'hastscript'
import {matches} from 'hast-util-select'
matches('b, i', h('b'))
matches(':any-link', h('a'))
matches(':any-link', h('a', {href: '#'}))
matches('.classy', h('a', {className: ['classy']}))
matches('#id', h('a', {id: 'id'}))
matches('[lang|=en]', h('a', {lang: 'en'}))
matches('[lang|=en]', h('a', {lang: 'en-GB'}))
select(selector, tree[, space])
Select the first element that matches selector
in the given tree
.
Searches the tree in preorder.
Parameters
selector
(string
)
— CSS selector, such as (h1
, a, b
)tree
(Node
, optional)
— tree to searchspace
(Space
, default: 'html'
)
— name of namespace
Returns
First element in tree
that matches selector
or null
if nothing is found.
This could be tree
itself.
Example
import {h} from 'hastscript'
import {select} from 'hast-util-select'
console.log(
select(
'h1 ~ :nth-child(even)',
h('section', [
h('p', 'Alpha'),
h('p', 'Bravo'),
h('h1', 'Charlie'),
h('p', 'Delta'),
h('p', 'Echo')
])
)
)
Yields:
{ type: 'element',
tagName: 'p',
properties: {},
children: [ { type: 'text', value: 'Delta' } ] }
selectAll(selector, tree[, space])
Select all elements that match selector
in the given tree
.
Searches the tree in preorder.
Parameters
selector
(string
)
— CSS selector, such as (h1
, a, b
)tree
(Node
, optional)
— tree to searchspace
(Space
, default: 'html'
)
— name of namespace
Returns
Elements in tree
that match selector
.
This could include tree
itself.
Example
import {h} from 'hastscript'
import {selectAll} from 'hast-util-select'
console.log(
selectAll(
'h1 ~ :nth-child(even)',
h('section', [
h('p', 'Alpha'),
h('p', 'Bravo'),
h('h1', 'Charlie'),
h('p', 'Delta'),
h('p', 'Echo'),
h('p', 'Foxtrot'),
h('p', 'Golf')
])
)
)
Yields:
[ { type: 'element',
tagName: 'p',
properties: {},
children: [ { type: 'text', value: 'Delta' } ] },
{ type: 'element',
tagName: 'p',
properties: {},
children: [ { type: 'text', value: 'Foxtrot' } ] } ]
Space
Namespace (TypeScript type).
Type
type Space = 'html' | 'svg'
Support
Unsupported
Notes
- * — not supported in
matches
- † — needs a user, browser, interactivity, or scripting to make sense
- ‡ — not supported by the underlying algorithm
- § — not very interested in writing / including the code for this
- ‖ — too new, the spec is still changing
Types
This package is fully typed with TypeScript.
It exports the additional type Space
.
Compatibility
Projects maintained by the unified collective are compatible with all maintained
versions of Node.js.
As of now, that is Node.js 14.14+ and 16.0+.
Our projects sometimes work with older versions, but this is not guaranteed.
Security
This package does not change the syntax tree so there are no openings for
cross-site scripting (XSS) attacks.
Related
Contribute
See contributing.md
in syntax-tree/.github
for
ways to get started.
See support.md
for ways to get help.
This project has a code of conduct.
By interacting with this repository, organization, or community you agree to
abide by its terms.
License
MIT © Titus Wormer